package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Advertisement;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.AdvertisementMapper;
import com.ruoyi.system.service.IAdvertisementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class AdvertisementServiceImpl implements IAdvertisementService {
    @Autowired
    private AdvertisementMapper advertisementMapper;

    /**
     * 查询广告
     *
     * @param advertisementId 广告主键
     * @return 广告
     */
    @Override
    public Advertisement selectAdvertisementByAdvertisementId(Integer advertisementId) {
        return advertisementMapper.selectAdvertisementByAdvertisementId(advertisementId);
    }

    /**
     * 查询广告弹窗消息
     *
     * @param advertisementId 广告主键
     * @return 广告
     */
    public AdvertisementRmarkDetail selectAdvertisementRemarkByAdvertisementId(Integer advertisementId) {
        return advertisementMapper.selectAdvertisementRemarkByAdvertisementId(advertisementId);
    }

    /**
     * 查询vip广告详情
     *
     * @param advertisementId 广告主键
     * @return 广告
     */
    @Override
    public AdvertisementVipDetail selectVipAdvertisementByAdvertisementId(Integer advertisementId) {
        return advertisementMapper.selectVipAdvertisementByAdvertisementId(advertisementId);
    }

    /**
     * 查询广告列表
     *
     * @param advertisement 广告
     * @return 广告
     */
    @Override
    public List<Advertisement> selectAdvertisementList(Advertisement advertisement) {
        return advertisementMapper.selectAdvertisementList(advertisement);
    }

    /**
     * 新增广告
     *
     * @param advertisement 广告
     * @return 结果
     */
    @Override
    public int insertAdvertisement(Advertisement advertisement) {
        return advertisementMapper.insertAdvertisement(advertisement);
    }

    /**
     * 修改广告
     *
     * @param advertisement 广告
     * @return 结果
     */
    @Override
    public int updateAdvertisement(Advertisement advertisement) {
        advertisement.setUpdateTime(DateUtils.getNowDate());
        return advertisementMapper.updateAdvertisement(advertisement);
    }

    /**
     * 批量删除广告
     *
     * @param advertisementIds 需要删除的广告主键
     * @return 结果
     */
    @Override
    public int deleteAdvertisementByAdvertisementIds(Integer[] advertisementIds) {
        return advertisementMapper.deleteAdvertisementByAdvertisementIds(advertisementIds);
    }

    /**
     * 删除广告信息
     *
     * @param advertisementId 广告主键
     * @return 结果
     */
    @Override
    public int deleteAdvertisementByAdvertisementId(Integer advertisementId) {
        return advertisementMapper.deleteAdvertisementByAdvertisementId(advertisementId);
    }


    /**
     * 查询vip广告列表
     *
     * @param advertisement 广告
     * @return 广告
     */
    @Override
    public List<AdvertisementVipVo> selectVipAdvertisementList(Advertisement advertisement) {
        return advertisementMapper.selectVipAdvertisementList(advertisement);
    }

    /**
     * 查询当前州的广告总数量
     *
     * @param advertisement 广告
     * @return 广告集合
     */
    public AdvertisementCount getAdvertisementCount(Advertisement advertisement) {
        AdvertisementCount advertisementCount = new AdvertisementCount();
        advertisementCount.setCityName("加州");
        advertisementCount.setAdvertisementCount("10");
        return advertisementCount;
//        return advertisementMapper.getAdvertisementCount(advertisement);
    }

    /**
     * 查询推荐广告列表
     *
     * @param advertisement 广告
     * @return 广告
     */
    @Override
    public List<AdvertisementRecommendVo> getRecommendList(Advertisement advertisement) {
        advertisement.setType("3");
        return advertisementMapper.getRecommendList(advertisement);
    }

//    /**
//     * 查询全部广告列表
//     *
//     * @param advertisement 广告
//     * @return 广告
//     */
//    @Override
//    public List<AdvertisementAllListVo> getAllList(Advertisement advertisement) {
//        Advertisement largeAllList = advertisement;
//        largeAllList.setType("2");
//        List<AdvertisementRecommendVo> getLargeALlList = advertisementMapper.getAllList(largeAllList);
//        Advertisement thumbnailALlList = advertisement;
//        thumbnailALlList.setType("1");
//        List<AdvertisementRecommendVo> getThumbnailALlList = advertisementMapper.getAllList(thumbnailALlList);
//
//        List<AdvertisementAllListVo> getAllList = new ArrayList<>();
//        AdvertisementAllListVo advertisementAllListVo = new AdvertisementAllListVo();
//        if (getLargeALlList.size() > 0) {
//
//            for (int i = 0, j = getLargeALlList.size(); i < j; i++) {
//                int num = 0;
//                AdvertisementAllListVo advertisementAllListVo1 = new AdvertisementAllListVo();
//                List<AdvertisementRecommendVo> getLargeALlListData = new ArrayList<>();
//                getLargeALlListData.add(getLargeALlList.get(i));
//                advertisementAllListVo1.setLargeALlList(getLargeALlListData);
//
//                for (int q = 0, s = getThumbnailALlList.size(); q < s; q++) {
//                    if (j == i && s != num) {
//                        List<AdvertisementRecommendVo> getThumbnailALlListData = new ArrayList<>();
//                        if (num / 4 != 1) {
//                            num++;
//                            getThumbnailALlListData.add(getThumbnailALlList.get(q));
//                        } else {
//                            System.out.println(num);
//                            System.out.println("num / 4:"+num / 4);
//                            num = 0;
//                            advertisementAllListVo.setLargeALlList(new ArrayList<>());
//                            advertisementAllListVo.setThumbnailALlList(getThumbnailALlListData);
//                            getAllList.add(advertisementAllListVo);
//                            num ++ ;
//                            getThumbnailALlListData.clear();
//                        }
//                    } else {
//                        List<AdvertisementRecommendVo> getThumbnailALlListData = new ArrayList<>();
//                        if (num / 4 != 1) {
//                            num++;
//                            getThumbnailALlListData.add(getThumbnailALlList.get(q));
//                        } else {
//                            System.out.println(num);
//                            System.out.println("num / 4:"+num / 4);
//                            num = 0;
//                            advertisementAllListVo.setLargeALlList(new ArrayList<>());
//                            advertisementAllListVo.setThumbnailALlList(getThumbnailALlListData);
//                            getAllList.add(advertisementAllListVo);
//                            num ++ ;
//                            getThumbnailALlListData.clear();
//                            continue;
//                        }
//                    }
//                }
//
//                getAllList.add(advertisementAllListVo1);
//            }
//
//        } else {
//            int num = 0;
//            List<AdvertisementRecommendVo> getThumbnailALlListData = new ArrayList<>();
//            for (int q = 0, s = getThumbnailALlList.size(); q < s; q++) {
//                if (num / 4 != 1) {
//                    num++;
//                    getThumbnailALlListData.add(getThumbnailALlList.get(q));
//                } else {
//                    System.out.println(num);
//                    System.out.println("num / 4:"+num / 4);
//                    num = 0;
//                    advertisementAllListVo.setLargeALlList(new ArrayList<>());
//                    advertisementAllListVo.setThumbnailALlList(getThumbnailALlListData);
//                    getAllList.add(advertisementAllListVo);
//                    num ++ ;
//                    getThumbnailALlListData.clear();
//                }
//            }
//            getAllList.add(advertisementAllListVo);
//        }
//        getAllList.stream().forEach(x->{
//            if(null == x.getLargeALlList()){
//                x.setLargeALlList(new ArrayList<>());
//            }
//            if(null == x.getThumbnailALlList()){
//                x.setThumbnailALlList(new ArrayList<>());
//            }
//        });
//        return getAllList;
//    }


    /**
     * 查询全部广告列表
     *
     * @param advertisement 广告
     * @return 广告
     */
    @Override
    public List<AdvertisementAllListVo> getAllList(Advertisement advertisement) {
        Advertisement largeAllList = advertisement;
        largeAllList.setType("2");
        List<AdvertisementRecommendVo> getLargeALlList = advertisementMapper.getAllList(largeAllList);
        Advertisement thumbnailALlList = advertisement;
        thumbnailALlList.setType("1");
        List<AdvertisementRecommendVo> getThumbnailALlList = advertisementMapper.getAllList(thumbnailALlList);

        List<List<AdvertisementRecommendVo>> getThumbnailALlListreturn = split(getThumbnailALlList,4);
        List<List<AdvertisementRecommendVo>> getLargeALlListreturn = split(getLargeALlList,1);
        List<AdvertisementAllListVo> getAllList = new ArrayList<>();
        if(getLargeALlListreturn.size() >= getThumbnailALlListreturn.size()){
            for(int i=0,j= getLargeALlListreturn.size();i<j;i++){
                AdvertisementAllListVo advertisementAllListVo = new AdvertisementAllListVo();
                advertisementAllListVo.setLargeALlList(getLargeALlListreturn.get(i));
                if(i+1<=getThumbnailALlListreturn.size()){
                    advertisementAllListVo.setThumbnailALlList(getThumbnailALlListreturn.get(i));
                }else{
                    List<AdvertisementRecommendVo> demo = new ArrayList<>();
                    advertisementAllListVo.setThumbnailALlList(demo);
                }
                getAllList.add(advertisementAllListVo);
            }
        }else{
            for(int i=0,j= getThumbnailALlListreturn.size();i<j;i++){
                AdvertisementAllListVo advertisementAllListVo = new AdvertisementAllListVo();
                advertisementAllListVo.setThumbnailALlList(getThumbnailALlListreturn.get(i));
                if(i+1<=getLargeALlListreturn.size()){
                    advertisementAllListVo.setLargeALlList(getLargeALlListreturn.get(i));
                }else{
                    List<AdvertisementRecommendVo> demo = new ArrayList<>();
                    advertisementAllListVo.setLargeALlList(demo);
                }
                getAllList.add(advertisementAllListVo);
            }
        }




        return getAllList;
    }

    public static <T> List<List<T>> split(List<T> resList, int count) {
        if (resList == null || count < 1)
            return null;
        List<List<T>> ret = new ArrayList<List<T>>();
        int size = resList.size();
        if (size <= count) {
            // 数据量不足count指定的大小
            ret.add(resList);
        } else {
            int pre = size / count;
            int last = size % count;
            // 前面pre个集合，每个大小都是count个元素
            for (int i = 0; i < pre; i++) {
                List<T> itemList = new ArrayList<T>();
                for (int j = 0; j < count; j++) {
                    itemList.add(resList.get(i * count + j));
                }
                ret.add(itemList);
            }
            // last的进行处理
            if (last > 0) {
                List<T> itemList = new ArrayList<T>();
                for (int i = 0; i < last; i++) {
                    itemList.add(resList.get(pre * count + i));
                }
                ret.add(itemList);
            }
        }
        return ret;
    }



}
